<template>
  <a-modal title="修改推广人" :after-close="reset" @ok="submit">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="推广人：">
        <a-select v-model:value="form.uid" placeholder="请选择" clearable filterable @change="setAdmin">
          <a-select-option v-for="(item, index) in admins" :key="index" :value="item.uid">
            {{ item.nickname }}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { adminList, updateSpreadApi } from "./api";
import { user, type Iuser } from './entity';
import { validReg } from '@/utils/validate';

const props = withDefaults(defineProps<{
  data: Partial<Iuser>
}>(), {
})
const { formRef, submit, close } = useDialog({ validCallback: save })

let form = $ref(new user())
watchEffect(() => {
  if (props.data) {
    form.userId = props.data.uid

  }
})

const rules = {
  usergroupName: [validReg('required', '请输入用户名')]
}

onMounted(() => {
  queryAdmin()
})
let admins = $ref<Iuser[]>([])
async function queryAdmin() {
  let { list } = await adminList({ page: 1, limit: 100 })
  admins = list
}
const emit = defineEmits(['change'])
function setAdmin(e: number) {
  let temp = admins.filter(item => item.uid == e)[0]
  form.image = temp.avatar
  form.spreadUid = temp.uid
}
function save() {
  updateSpreadApi(form).then(() => {
    AntMessage.success('保存成功')
    close()
    emit('change')
  })
}

function reset() {
  form = new user()
}
</script>

<style lang="less"></style>
